﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Kaixin.BLL.Model;

namespace Kaixin.BLL
{
    public class Config
    {
        private XDocument _xmlConfig;
        private static Config _config;
        private const string __fileName = "ConfigDEMO.xml";
        /// <summary>
        /// private construction
        /// </summary>
        private Config()
        {
            FileInfo f = new FileInfo(__fileName);
            if (!f.Exists)
            {
                using (StreamWriter sw = new StreamWriter(f.Create()))
                {
                    sw.Write("<?xml version=\"1.0\" encoding=\"utf-8\" ?><kaixin><myAccount></myAccount><firends></firends><VisitInterval>500</VisitInterval></kaixin>");
                }
            }
            _xmlConfig = XDocument.Load(__fileName);
        }
        /// <summary>
        /// singleton 
        /// </summary>
        /// <returns></returns>
        public static Config CreateConfig()
        {
            if (_config == null)
            {
                _config = new Config();
            }
            return _config;
        }

        /// <summary>
        /// 请求访问间隔
        /// 未设置或设置错误返回1000
        /// </summary>
        public int VisitInterval() {
            string interval = _xmlConfig.Root.Element("VisitInterval").Value;
            int ret ;
            return int.TryParse(interval, out ret) ? ret : 1000;
        }
        /// <summary>
        /// 返回好友列表
        /// </summary>
        /// <returns></returns>
        public List<User> FirendList
        {
            get
            {
                var f = from firend in _xmlConfig.Root.Element("firends").Descendants() select firend;
                List<User> list = new List<User>();
                foreach (var xElement in f)
                {
                    User model = new User
                                     {
                                         Email = string.Empty,
                                         Password = string.Empty,
                                         Name = xElement.Value,
                                         UID = xElement.Attribute("id").Value
                                     };
                    list.Add(model);
                }
                return list;
            }
            set
            {
                XElement firend = _xmlConfig.Root.Element("firends");
                firend.RemoveAll();
                foreach (var user in value)
                {
                    XElement element = new XElement("user", user.Name);
                    element.SetAttributeValue("id", user.UID);
                    firend.Add(element);
                }
                _xmlConfig.Save(__fileName);
            }
        }
      
        /// <summary>
        /// 如果文件存在则返回不存在则创建
        /// </summary>
        public static void ConfigFileCreate(string fileName,string defaultTxt)
        {
            FileInfo file = new FileInfo(fileName);
            if (!file.Exists)
            {
                StreamWriter sw = new StreamWriter(file.Create());
                sw.Write(defaultTxt);
                sw.Close();
            }
        }
      
    }
}
